<%@page import="com.netistate.pojo.LaborStat"%>
<%@page import="com.netistate.action.LaborAction"%>
<%@page import="org.apache.struts2.ServletActionContext"%>
<%@page import="com.opensymphony.xwork2.ActionContext"%>
<%@page language="java" pageEncoding="utf-8"%>

<%@page import="java.util.List"%>
<%@page import="java.io.IOException"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFRichTextString"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFFont"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFCellStyle"%>
<%@page import="org.apache.poi.hssf.util.HSSFColor"%>
<%@page import="org.apache.poi.hssf.util.CellRangeAddress"%>
<%@page import="java.util.ArrayList"%>
<%
  String projectName1=request.getParameter("projectName");
  projectName1=URLDecoder.decode(projectName1,"UTF-8");

 %>

<%!private void createEventExcel(ServletOutputStream os,
        String sheetTitle, boolean isHistoryEvent,String projectName12) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet(sheetTitle);
    //设置表格宽度
    sheet.setColumnWidth(0,5000);
    sheet.setColumnWidth(1,2000);
    sheet.setColumnWidth(2,2000);
    sheet.setColumnWidth(3,2000);
    //给表单添加表单
    HSSFCellStyle style=wb.createCellStyle();
    HSSFFont font=wb.createFont();
    font.setFontName("Arial");
    font.setFontHeightInPoints((short) 12);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中   
    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上线居中   
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);   
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);   
    style.setBorderTop(HSSFCellStyle.BORDER_THIN);   
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);   
    style.setWrapText(true);   
    style.setFont(font);  
    //标题的样式设置
    HSSFCellStyle titleStyle=wb.createCellStyle();
    HSSFFont titleFont=wb.createFont();
    titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    titleFont.setFontName("宋体");
    titleFont.setFontHeightInPoints((short)12);
    titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中   
    titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上线居中   
    titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);   
    titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);   
    titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);   
    titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);   
    titleStyle.setWrapText(true);
    titleStyle.setFillPattern(HSSFCellStyle.DIAMONDS);
    titleStyle.setFillForegroundColor(HSSFColor.BLUE_GREY.index);
    titleStyle.setFillBackgroundColor(HSSFColor.BLUE_GREY.index);
    titleStyle.setFont(titleFont);
    
    /* exportSearch ee=new exportSearch();
    List<ExcelData> list=ee.searchExportByContent(); */
    ActionContext ctx = ActionContext.getContext(); 
  	HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);  
    List<LaborStat> list = (List<LaborStat>)request.getSession().getAttribute("laborStatList");
    int rownum = 0;
    HSSFRow header = sheet.createRow((short) rownum++);
    header.setHeight((short) 600);
    int headerIndex = 0;
    this.writeCell(header, headerIndex++, "项目名称",titleStyle);
    this.writeCell(header, headerIndex++, "工时汇总",titleStyle);
    this.writeCell(header, headerIndex++, "内容",titleStyle);
    this.writeCell(header, headerIndex++, "工时",titleStyle);
    this.writeCell(header, headerIndex++, "现场人次",titleStyle);

    //计算每一个项目的总的工时
    String strProjectName="";
    String projectNameLast = "";
    float f=0;
    int n=0;
    //List<ExcelData> list2 = new ArrayList<ExcelData>();
    List<LaborStat> list2 = new ArrayList<LaborStat>();
    for(int j=0;j<list.size();j++){
       //ExcelData excelData = list.get(j);
       LaborStat excelData = list.get(j);
      // System.out.println(excelData.getProjectName()+"  "+excelData.getWorkTime());
      if(list.size()==1){
         list2.add(list.get(0));
      }else{
		   if(j==0){
		       projectNameLast = excelData.getProjectName();
		       f = Float.parseFloat(excelData.getWorkTime());
		   }else if(j==(list.size()-1)){
		       if(excelData.getProjectName().equals(projectNameLast)){
		            //ExcelData excelData2 = new ExcelData();
		            LaborStat excelData2 = new LaborStat();
		            excelData2.setProjectName(projectNameLast);
		            excelData2.setWorkTime(f+excelData.getWorkTime());
		            list2.add(excelData2);
		       }else{
		            //ExcelData excelData2 = new ExcelData();
		            LaborStat excelData2 = new LaborStat();
		            excelData2.setProjectName(projectNameLast);
		            excelData2.setWorkTime(f+"");
		            //ExcelData excelData3 = new ExcelData();
		            LaborStat excelData3 = new LaborStat();
		            excelData3.setProjectName(excelData.getProjectName());
		            excelData3.setWorkTime(excelData.getWorkTime());
		            list2.add(excelData2);
		            list2.add(excelData3);
		       }
		   }else{
		       if(excelData.getProjectName().equals(projectNameLast)){
		             f = f+Float.parseFloat(excelData.getWorkTime());
		       }else{
		           //ExcelData excelData2 = new ExcelData();
		           LaborStat excelData2 = new LaborStat();
		           excelData2.setProjectName(projectNameLast);
		           excelData2.setWorkTime(f+"");
		           list2.add(excelData2);
		           projectNameLast = excelData.getProjectName();
		           f=Float.parseFloat(excelData.getWorkTime());
		       }
		   }
	   }
    }
    
    for (int i=0;i<list.size();i++) {
	        //ExcelData excel=list.get(i);
	        LaborStat excel=list.get(i);
	        String projectName=excel.getProjectName();
	        float workTime=Float.parseFloat(excel.getWorkTime());
	        String s=Float.toString(workTime);
	        s=s.substring(0,s.indexOf('.'));
	        int worktime=Integer.parseInt(s);

	        int currentPerson=Integer.parseInt(excel.getCurrentPerson());
	        String content=excel.getSection();
	        int size=0;
	        if(!projectName12.equals("所有")){
	             if(!projectName12.equals(projectName)){
	             continue;
	           }
	        }
	        int cellnum = 0;

	        HSSFRow row = sheet.createRow((short) rownum++);
	        
	        if(i==0){
	            strProjectName = projectName;
	        }else if(i==(list.size()-1)){
	             if(strProjectName.equals(projectName)){
	                sheet.addMergedRegion(new CellRangeAddress(i,i+1,0,0));
		            sheet.addMergedRegion(new CellRangeAddress(i,i+1,1,1));
	             }
	        }else{
	            if(!strProjectName.equals(projectName)){
		             strProjectName=projectName;
		            
		        }else{
		            
		            sheet.addMergedRegion(new CellRangeAddress(i,i+1,0,0));
		            sheet.addMergedRegion(new CellRangeAddress(i,i+1,1,1));
		            strProjectName = projectName;
		        }
	        }
	        this.writeCell(row, cellnum++, projectName,style);
	        float countTime=0;
	        for(int m=0;m<list2.size();m++){
	             //ExcelData excelData =list2.get(m);
	             LaborStat excelData =list2.get(m);
	             if(projectName.equals(excelData.getProjectName())){
	                  countTime=Float.parseFloat(excelData.getWorkTime());
	                  
	             }
	        }
	        
	        String str =Float.toString(countTime);
	        str=str.substring(0,str.indexOf('.'));
	        int workstart=Integer.parseInt(str);
	        if(countTime-workstart==0){
	            this.writeCell(row, cellnum++, workstart+"",style);
	        }else{
	           this.writeCell(row, cellnum++, countTime+"",style);
	        }
	        
	        this.writeCell(row, cellnum++, content,style);
	        if(workTime-worktime==0){
	             this.writeCell(row, cellnum++, worktime+"",style);
	        }else{
	             this.writeCell(row, cellnum++, workTime+"",style);
	        }
	        this.writeCell(row, cellnum++, currentPerson+"",style);
	    }
    wb.write(os);
    os.flush();
    os.close();
}

private void writeCell(HSSFRow row, int cellnum, String content,HSSFCellStyle style) {
    HSSFCell cell = row.createCell(cellnum);
    HSSFRichTextString rs = new HSSFRichTextString(content);
    cell.setCellStyle(style);
    cell.setCellValue(rs);
}%>

<!-- 导出 -->
<%
String title = "工作内容工时统计报表";
String fileName = title + ".xls";
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="
        + new String(fileName.getBytes(), "iso-8859-1"));
createEventExcel(response.getOutputStream(),title,true,projectName1);

out.clear();
out = pageContext.pushBody();
%>